package beust;

import java.text.DecimalFormat;
import static beust.BobsBeustSequence.findAll;
import static beust.ModifiedBeustSequence.countTo;

/**
 *
 * @author jonathan
 */
public class CompareBeustSequence {

  static final long max = 10000000000L;
  static final int base = 10;
  static final Listener hearNoEvil = new Listener() {
      public void hear(long value) {}
  };

  static int numberOfNumbers() {
      CountingListener bobsCounter = new CountingListener();
      CountingListener modifiedCounter = new CountingListener();
      findAll(max, bobsCounter);
      countTo(max, base, modifiedCounter);
      assert (bobsCounter.count == modifiedCounter.count);
      return bobsCounter.count;
  }

  static class CountingListener implements Listener {
      int count; // trust me, it fits
      public void hear(long value) { count++; }
  }

  static long bobsTimes10() {
      long finish, start = System.nanoTime();
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      findAll(max, hearNoEvil);
      finish = System.nanoTime();
      return (finish - start) / 1000000;
  }
  
  static long modifiedTimes10() {
      long finish, start = System.nanoTime();
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      countTo(max, base, hearNoEvil);
      finish = System.nanoTime();
      return (finish - start) / 1000000;
  }
        
  public static void main(String[] args) {

      System.out.println("There are " + numberOfNumbers() +
              " unique numbers in the sequence.");
      
      long bobTime = 0;
      long modifiedTime = 0;

      bobTime = bobsTimes10();
      modifiedTime = modifiedTimes10();

      /*
       * I get modified times 13-19% faster
       * on my Intel Core 2 Duo 2.0 Ghz MacBook
       */
      System.out.println("Bob's time: " + bobTime);
      System.out.println("Modified time: " + modifiedTime);
      double pctDiff = ((double)bobTime / modifiedTime) - 1.0;
      System.out.println("Modified is " +
              new DecimalFormat("0.00%").format(pctDiff) +
              (pctDiff > 0 ? " faster " : " slower ") +
              "than Bob's.");
  }
}
